var userModel = require('../models/mongodbModel');
// 列出
exports.userList = function (req, res, next) {
    // var user = new userModel();
    // if(err){
    //     res.send(err);
    // }
    userModel.find({}).exec(function (err, docs) {
        console.log(docs);
        if (err) {
            console.log('Failed to list user!');
            return res.send({
                status: '404',
                message: '失败',
            });
        } else {
            return res.send({
                status: '200',
                message: '成功',
                data: docs
            });
        }
    });
};
// exports.findUser = function (req, res, next) {
//     // var user = new userModel();
//     // if(err){
//     //     res.send(err);
//     // }
//     console.log(req.body.findUserName);
//     userModel.findOne({'userName' : req.body.findUserName}).exec(function (err, docs) {
//         console.log(docs);
//         if (err) {
//             console.log('Failed to list user!');
//             return res.send({
//                 status: '404',
//                 message: '失败',
//             });
//         } else {
//             return res.send({
//                 status: '200',
//                 message: '成功',
//                 data: docs
//             });
//         }
//     });
// };
// 添加
exports.addUser = function (req, res, next) {
    console.log(req.body);
    // var user = new userModel.userModel(req.body.addUser);
    // if(err){
    //     res.send(err);
    // }
    var newUser = {
        "userName": req.body.username,
        "userInfo": {
            "age": req.body.age,
            "sex": req.body.sex,
        },
        "userHobby": [req.body.hobby1, req.body.hobby2, req.body.hobby3]
    };
    userModel.create(newUser, function (err, docs) {
        if (err) {
            console.log('Failed to add user!');
            return res.send({
                status: '404',
                message: '添加失败',
                data: docs
            });
        } else {
            return res.send({
                status: '200',
                message: '添加成功',
                data: docs
            });
        }
    });
};
// 更新
exports.updateUser = function (req, res, next) {
    console.log(req.body);
    //查询的条件
    var whereUser = { 'userName': req.body.updateUserName };
    //更新的内容
    var updateData = {
        $set: {
            "userName": req.body.updateUserName,
            "userInfo": {
                "age": req.body.updateAge,
                "sex": req.body.updateSex,
            },
            "userHobby": [req.body.updateHobby1, req.body.updateHobby2, req.body.updateHobby3]
        }
    };
    // res.send(updateData);
    //对数据库进行更新
    userModel.findOneAndUpdate(whereUser, {
        $set: {
            "userName": req.body.updateUserName,
            "userInfo": {
                "age": req.body.updateAge,
                "sex": req.body.updateSex,
            },
            "userINfo" : "",
            "userHobby": [req.body.updateHobby1, req.body.updateHobby2, req.body.updateHobby3]
        }
    }, function (error) {
        if (error) {
            console.log('数据修改失败:' + error);
            return res.send({
                status: '404',
                message: '修改失败',
                data: req.body
            });
        }
        else {
            console.log('数据修改成功');
            return res.send({
                status: '200',
                message: '修改成功',
                data: updateData
            });
        }
    });
    // userModel.update(whereUser,updateData,function (error) {
    //     if (error){
    //         console.log('数据修改失败:'+error)
    //         res.send({
    //             status:'y',
    //             message:'修改失败',
    //             data:req.body
    //         });
    //     }
    //     else{
    //         console.log('数据修改成功')
    //         res.send({
    //             status:'y',
    //             message:'修改成功',
    //             data:req.body
    //         });
    //     }
    // });
};
// 删除
exports.deleteUser = function (req, res, next) {
    console.log(req.body.username);
    var username = req.body.username;
    userModel.remove({ 'userName': username }, function (error) {
        if (error) {
            console.log('数据获取失败' + error);
            return res.json({
                status: '404',
                message: '删除失败',
            });
        }
        else {
            return res.send({
                status: '200',
                message: '删除成功',
            });
        }
    });
};